package com.xx.ems.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import com.xx.ems.util.impl.ObjectTransformImpl;

/**
 * @author xxx
 * @date 2014-2-25
 * @version 1.0
 * 描述: 角色表
 */
@Entity
public class Roles {
	/**
	 * 描述：主键
	 */
	private Integer rid;
	/**
	 * 描述：角色名
	 */
	private String rolename;
	/**
	 * 描述：创建人
	 */
	private Users creater;
	/**
	 * 描述：创建时间
	 */
	private Date creation_time;
	/**
	 * 描述：修改人
	 */
	private Users changer;
	/**
	 * 描述：修改时间
	 */
	private Date change_time;
	/**
	 * 描述：备注
	 */
	private String comment;
	/**
	 * 描述：用户表关联对象
	 */
	private Set<Users> users = new HashSet<Users>();
	/**
	 * 描述：动作表关联对象
	 */
	private Set<Action> action = new HashSet<Action>();
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="rid",unique=true,nullable=false)
	public Integer getRid() {
		return rid;
	}
	public void setRid(Integer rid) {
		this.rid = rid;
	}
	@Column(name="rolename",length=20,nullable=false)
	public String getRolename() {
		return rolename;
	}
	public void setRolename(String rolename) {
		this.rolename = rolename;
	}
	@ManyToOne(targetEntity=Users.class)
	@JoinColumn(name="fk_creater")
	public Users getCreater() {
		return creater;
	}
	public void setCreater(Users creater) {
		this.creater = creater;
	}
	@Column(name="creation_time",nullable=false)
	public Date getCreation_time() {
		return creation_time;
	}
	public void setCreation_time(Date creation_time) {
		this.creation_time = creation_time;
	}
	@ManyToOne(targetEntity=Users.class)
	@JoinColumn(name="fk_changer")
	public Users getChanger() {
		return changer;
	}
	public void setChanger(Users changer) {
		this.changer = changer;
	}
	@Column(name="change_time")
	public Date getChange_time() {
		return change_time;
	}
	public void setChange_time(Date change_time) {
		this.change_time = change_time;
	}
	@Column(name="comment",length=255)
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	@ManyToMany(targetEntity=Users.class,mappedBy="roles",cascade=CascadeType.ALL)
	public Set<Users> getUsers() {
		return users;
	}
	public void setUsers(Set<Users> users) {
		this.users = users;
	}
	@ManyToMany(targetEntity=Action.class,mappedBy="roles",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	public Set<Action> getAction() {
		return action;
	}
	public void setAction(Set<Action> action) {
		this.action = action;
	}
	/**
	 * 描述: 将实体的属性及相应的值转换成Map类型(为了方便转换成json格式后前台调用) 
	 * @return
	 * @date 2014-3-4
	 */
	public Map<String, Object> toHashMap() {
		Map<String, Object> map = new ObjectTransformImpl().javabeanToMap(this);
		map.put("creater", getCreater().getUsername());
		map.put("changer", getChanger().getUsername());
		return map;
	}
}